//
//  CmtRECTableViewController.m
//  Products_B
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CmtRECTableViewController.h"

#import "CmtShenQuViewController.h"
#import "CmtProductDetailViewController.h"//作品详情

#import "ScrollViewManager.h"
#import "RequestManager.h"
#import "CmtRECMarrow.h"
#import "CmtRECModel.h"
#import "CmtRECTalent.h"
#import "CmtTopicsDataModel.h"
#import "CmtRECTopics.h"
#import "imageLabel.h"
#import "CmtRecTableViewCell.h"
#import "ImageUpLabel.h"

#import "CmtCollectionViewController.h"
#import "CmtShenQuListViewController.h"

//#define kCmtRecUrlString @"http://api.izhangchu.com/?=token=289466943102E6204C7EE83EF061FCE4&methodName=ShequRecommend&user_id=1514218&version=4.40&"
#define kCmtRecLogUrlString @"http://api.izhangchu.com/?=methodName=ShequRecommend&version=4.40&"
@interface CmtRECTableViewController ()

@property(nonatomic,strong)NSMutableArray *modelArray;
@property(nonatomic,strong)NSMutableArray *CmtRecTalentArr;
@property(nonatomic,strong)NSMutableArray *CmtRecMarrowArr;
@property(nonatomic,strong)NSMutableArray *CmtRecTopicsArr;
@property(nonatomic,strong)NSMutableArray *CmtTopDataArray;


@property(nonatomic,assign)CGPoint OldContentOffset;
@property(nonatomic,assign)CGPoint NewContentOffset;
@property(nonatomic,assign)CGPoint ContentOffset;

@end

@implementation CmtRECTableViewController
-(void)viewWillLayoutSubviews{
    
    self.tableView.frame = CGRectMake(kWidth, 0, kWidth, kHeight -  50 - 64);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor redColor];
    
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 100)];
    self.tableView.tableHeaderView.backgroundColor = [UIColor yellowColor];
    
    self.modelArray = [NSMutableArray arrayWithCapacity:10];
    self.CmtRecMarrowArr = [NSMutableArray arrayWithCapacity:10];
    self.CmtRecTalentArr = [NSMutableArray arrayWithCapacity:10];
    self.CmtRecTopicsArr = [NSMutableArray arrayWithCapacity:10];
    self.CmtTopDataArray = [NSMutableArray arrayWithCapacity:10];
    
    [self requestDataWithUrlString:kCmtRecLogUrlString];
    
    [self.tableView registerClass:[CmtRecTableViewCell class] forCellReuseIdentifier:@"recCell"];
    
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    NSLog(@"开始拖拽%lf",scrollView.contentOffset.y);
    self.ContentOffset = scrollView.contentOffset;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //    NSLog(@"滚动%lf",scrollView.contentOffset.y);
    self.NewContentOffset = scrollView.contentOffset;
    
    //    向下滑动
    if (self.NewContentOffset.y > self.ContentOffset.y | self.NewContentOffset.y > self.OldContentOffset.y) {
        NSLog(@"向下滑");
        
        self.block(YES);
        
    }else{
        
        NSLog(@"向上滑");
        self.block(NO);
    }
    
    
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"开始减速%lf",scrollView.contentOffset.y);
    self.OldContentOffset = scrollView.contentOffset;
}



//数据请求
-(void)requestDataWithUrlString:(NSString *)urlString{

    NSDictionary *pardic = @{@"methodName":@"ShequRecommend",@"version":@"4.40&"};
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:pardic finish:^(NSData *data) {
        
        NSDictionary *jsDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];

        NSMutableArray *imageArr = [NSMutableArray arrayWithCapacity:10];
        
        NSArray *bannerArray = jsDic[@"data"][@"banner"];
        for (NSDictionary *dic in bannerArray) {
            CmtRECModel *model = [[CmtRECModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_modelArray addObject:model];
            [imageArr addObject:[model.banner_picture stringByReplacingOccurrencesOfString:@"\\" withString:@""]];
        }
        
        [ScrollViewManager viewRepeatScrollWithPicArray:imageArr view:self.tableView.tableHeaderView frame:CGRectMake(0, 0, kWidth, 100)tapBlock:nil];

        NSArray *tablentArray = jsDic[@"data"][@"shequ_talent"];
        for (NSDictionary *dic in tablentArray) {
            CmtRECTalent *model = [[CmtRECTalent alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_CmtRecTalentArr addObject:model];
        }
        NSArray *marrowArray = jsDic[@"data"][@"shequ_marrow"];
        for (NSDictionary *dic in marrowArray) {
            CmtRECMarrow *model = [[CmtRECMarrow alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_CmtRecMarrowArr addObject:model];
        }
        NSArray *topicsArray = jsDic[@"data"][@"shequ_topics"];
        for (NSDictionary *dic in topicsArray) {
            CmtRECTopics *TopModel = [[CmtRECTopics alloc]init];
            [TopModel setValuesForKeysWithDictionary:dic];
            [_CmtRecTopicsArr addObject:TopModel];
        }
        [self.tableView reloadData];
        
    } error:nil];

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else{
    
        return _CmtRecTopicsArr.count;
    }
}

//返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CmtRecTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recCell" forIndexPath:indexPath];
      cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.section == 0) {//达人
        
        [cell setWithModelArray:_CmtRecTalentArr TapBlock:^(int index){
            int count = index;
            CmtRECTalent *model = _CmtRecTalentArr[count];
            
            CmtShenQuViewController *CmtSQVc = [[CmtShenQuViewController alloc]init];
            CmtSQVc.user_id = model.user_id;
            
            [self.navigationController pushViewController:CmtSQVc animated:YES];
            
        }];
        return cell;
    }
    if (indexPath.section == 1) {//精品
        
        [cell setScrollewWith:_CmtRecMarrowArr];
        cell.tapIndexBlock = ^(int index){
        
            CmtRECMarrow *model = _CmtRecMarrowArr[index];
            CmtProductDetailViewController *CmtSecDetailVc = [[CmtProductDetailViewController alloc]init];
            CmtSecDetailVc.post_id = model._id;
            [self.navigationController pushViewController:CmtSecDetailVc animated:YES];

        };
        
        return cell;
    }
    else{//第三分区作品
//        跳转到作品详情页
        CmtRECTopics *topModel = _CmtRecTopicsArr[indexPath.row];
        [cell setCellWithModel:topModel];
        
        [cell.button addTarget:self action:@selector(CellButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        cell.button.tag = indexPath.row + 100;
        
        
        cell.tapIndexBlock = ^(int index){
        
            CmtTopicsDataModel *mode = topModel.dataArr[index];
            CmtProductDetailViewController *CmtSecDetailVc = [[CmtProductDetailViewController alloc]init];
            CmtSecDetailVc.post_id = mode.data_id;
        
            [self.navigationController pushViewController:CmtSecDetailVc animated:YES];
            
        };
    
    }
    return cell;
}

-(void)CellButtonAction:(UIButton *)button{

    NSInteger index = button.tag - 100;
    
    CmtRECTopics *marrow = self.CmtRecTopicsArr[index];
    
    CmtShenQuListViewController *CmtSQListVc = [[CmtShenQuListViewController alloc]init];
    CmtSQListVc.is_marrow = @"0";
    CmtSQListVc.topic_id = marrow._id;
    CmtSQListVc.titleString = [NSString stringWithFormat:@"%@",marrow.topic_name];
    [self.navigationController pushViewController:CmtSQListVc animated:YES];
    
    
}

//返回Cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        return kWidth / 4 + 80;
    }
    if (indexPath.section == 1) {
        return kWidth / 4 ;
    }
    return  (kWidth / 3 +  50);
}

//返回区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section == 0) {//达人
        UIView *sectionV0 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 30)];
        sectionV0.backgroundColor = [UIColor whiteColor];
        ImageUpLabel *imageLabel0 = [[ImageUpLabel alloc]initWithFrame:CGRectMake(10, 0, 90, 30) image:[UIImage imageNamed:@"Cmt1.png"] textLabel:@"达人" tapAction:^(int index){
        
        
        }];
        [sectionV0 addSubview:imageLabel0];
        return sectionV0;
    }
    if (section == 1) {//精品
        UIView *sectionV1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 30)];
        sectionV1.backgroundColor = [UIColor whiteColor];
        ImageUpLabel *imageLabel1 = [[ImageUpLabel alloc]initWithFrame:CGRectMake(10, 0, 180, 30) image:[UIImage imageNamed:@"cmt2.png"] textLabel:@"精选作品" tapAction:^(int index){
            CmtShenQuListViewController *CmtSQListVc = [[CmtShenQuListViewController alloc]init];
          
            CmtSQListVc.is_marrow = [NSString stringWithFormat:@"%d",1];
            [self.navigationController pushViewController:CmtSQListVc animated:YES];
            
        }];
        
        [sectionV1 addSubview:imageLabel1];
        return sectionV1;
    }
    
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 0)];
}
//返回区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section < 2) {
        return 30;
   }
    return 1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
